package com.attunlcok.unlock.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.attunlcok.unlock.mapper.AttUnlockRecordMapper;
import com.attunlcok.unlock.domain.AttUnlockRecord;
import com.attunlcok.unlock.service.IAttUnlockOrderService;

/**
 * 解锁服务业务层处理
 */
@Service
public class AttUnlockOrderServiceImpl implements IAttUnlockOrderService {
    
    @Autowired
    private AttUnlockRecordMapper attUnlockRecordMapper;

    /**
     * 根据IMEI创建解锁记录
     *
     * @param imei IMEI号码
     * @return 订单号
     */
    @Override
    public String createAttUnlockOrder(String imei) {
        if (imei == null || imei.isEmpty()) {
            throw new RuntimeException("IMEI号码不能为空");
        }
        
        // 生成唯一订单号
        String orderId = UUID.randomUUID().toString().replace("-", "");
        
        // 创建解锁记录
        AttUnlockRecord record = new AttUnlockRecord();
        record.setOrderId(orderId);
        record.setImei(imei);
        record.setUnlockStatus("0"); // 待处理
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        
        // 保存解锁记录
        attUnlockRecordMapper.insertAttUnlockRecord(record);
        
        return orderId;
    }

    /**
     * 根据订单号查询解锁记录
     *
     * @param orderId 订单号
     * @return 解锁记录
     */
    @Override
    public AttUnlockRecord selectUnlockRecordByOrderId(String orderId) {
        List<AttUnlockRecord> records = attUnlockRecordMapper.selectUnlockRecordsByOrderId(orderId);
        if (records != null && !records.isEmpty()) {
            return records.get(0);
        }
        return null;
    }
    
    /**
     * 根据订单号查询所有解锁记录
     *
     * @param orderId 订单号
     * @return 解锁记录列表
     */
    @Override
    public List<AttUnlockRecord> selectAllRecordsByOrderId(String orderId) {
        return attUnlockRecordMapper.selectUnlockRecordsByOrderId(orderId);
    }
    
    /**
     * 更新解锁记录的请求编号
     *
     * @param orderId 订单号
     * @param requestNumber 请求编号
     * @return 更新结果(影响行数)
     */
    @Override
    @Transactional
    public int updateRequestNumber(String orderId, String requestNumber) {
        if (orderId == null || orderId.isEmpty()) {
            throw new RuntimeException("订单号不能为空");
        }
        if (requestNumber == null || requestNumber.isEmpty()) {
            throw new RuntimeException("请求编号不能为空");
        }
        
        // 获取解锁记录
        AttUnlockRecord record = selectUnlockRecordByOrderId(orderId);
        if (record == null) {
            throw new RuntimeException("未找到对应的解锁记录");
        }
        
        // 更新请求编号
        record.setRequestNumber(requestNumber);
        record.setUpdateTime(new Date());
        
        // 同时更新状态为已确认(3)
        record.setUnlockStatus("3");
        
        return attUnlockRecordMapper.updateAttUnlockRecord(record);
    }
} 